<?php

/**
 * This is the model class for table "measurements".
 *
 * The followings are the available columns in table 'measurements':
 * @property integer $id
 * @property integer $deviceSequence
 */
class Measurement extends ActiveRecord {

	public $firstDateTime = null;
	public $lastDateTime = null;

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Measurement the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'measurements';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		$rules = parent::rules();
		$rules[] = array(
			array(
				'deviceSequence',
			),
			'numerical',
			'integerOnly'=>true,
		);
		$rules[] = array(
			array(
				'id',
				'deviceSequence',
				'createdAt',
				'updatedAt',
				'firstDateTime',
				'lastDateTime',
			),
			'safe',
			'on'=>'search',
		);
		return $rules;
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		$relations = parent::relations();
		$relations['channels'] = array(
			self::HAS_MANY,
			'MeasurementChannel',
			'measurementId',
			'order'=>'channelSequence',
		);
		return $relations;
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'id'=>'ID',
			'deviceSequence'=>'Device',
			'firstDateTime'=>'Start',
			'lastDateTime'=>'Finish',
			'createdAt'=>'Time',
		);
	}
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models
	 *   based on the search/filter conditions.
	 */
	public function search() {
		$criteria = new CDbCriteria;
		$criteria->compare('id', $this->id);
		$criteria->compare('deviceSequence', $this->deviceSequence);
		$criteria->compare('createdAt', $this->createdAt, true);
		$criteria->compare('updatedAt', $this->updatedAt, true);
		if (!empty($this->firstDateTime) && !empty($this->lastDateTime)){
			$criteria->addBetweenCondition('createdAt', $this->firstDateTime,
					$this->lastDateTime);
		}
		return new CActiveDataProvider($this, array('criteria'=>$criteria));
	}
	
	public function addChannel($channel) {
		$channel->measurementId = $this->id;
		return $channel->save();
	}
	
	protected $_arrayChannels = array();
	
	public function getArrayChannels() {
		if (!$this->_arrayChannels) {
			foreach ($this->channels as $channel) {
				$this->_arrayChannels[$channel->channelSequence] = array(
					'raw'=>$channel->value,
					'cute'=>$channel->cuteValue,
				);
			}
			for ($i = 1; $i <= 8; $i++) {
				if (!array_key_exists($i, $this->_arrayChannels)) {
					$this->_arrayChannels[$i] = array(
						'raw'=>null,
						'cute'=>null,
					);
				}
			}
		}
		return $this->_arrayChannels;
	}

}